package org.dydl.common.config;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {

	private final Log logger = LogFactory.getLog(getClass());

	// @ExceptionHandler(value = Throwable.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public void defaultErrorHandler(HttpServletRequest req, HttpServletResponse res, Exception e) {

		try {
			logger.error(e + " ------- " + Arrays.toString(e.getStackTrace()));
			res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "服务器错误！");
		} catch (Exception e2) {
			logger.error(e2 + "-------" + Arrays.toString(e.getStackTrace()));
		}

	}

	// @ExceptionHandler(value = MyException.class)
	// @ResponseBody
	// public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, MyException
	// e) throws Exception {
	// ErrorInfo<String> r = new ErrorInfo<>();
	// r.setMessage(e.getMessage());
	// r.setCode(ErrorInfo.ERROR);
	// r.setData("Some Data");
	// r.setUrl(req.getRequestURL().toString());
	// return r;
	// }

	// @ExceptionHandler(value = NoHandlerFoundException.class)
	// public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e)
	// throws Exception {
	// ModelAndView mav = new ModelAndView();
	// mav.addObject("exception", e);
	// mav.addObject("url", req.getRequestURL());
	// mav.setViewName("error");
	// return mav;
	// }
}